package com.wwwarehouse.contract.contract.info;

import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.InfoCardCompulsoryBean;
import com.wwwarehouse.contract.bean.InfoDeliverableItemBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/11/3.
 * 信息卡 义务项
 */

public class InfoCardCompulsoryFragment extends TheParentFragment {
	private TextView mTimeTv;
	private TextView mJudgeTv;
	private TextView mTitleTv;
	private int REQUEST_COMPULSORY_INFO_CODE = 1;
	@Override
	protected int getContentId() {
		return R.layout.info_card_compulsory_item_fragment;
	}

	@Override
	public void requestDatas() {
		if (getArguments() == null || StringUtils.isNullString(getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID))) {
			mLoadingView.showSystemView(true);
			mLoadingView.setSystemListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					requestDatas();
				}
			});
			return;
		}
		String mContractUkid = getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID);
		Map<String, String> mParamsMap = new HashMap<>();
		mParamsMap.put("contractUkid", mContractUkid);
		httpPost(ContractConstant.LOAD_OBLIGATION ,mParamsMap ,REQUEST_COMPULSORY_INFO_CODE );

	}

	@Override
	public void setTitle() {
		mActivity.setTitle(R.string.contract_modify_info_card_obligation_item_title);
	}

	@Override
	protected void init(View view) {
		mTimeTv = (TextView) view.findViewById(R.id.time_tv);
		mJudgeTv = (TextView) view.findViewById(R.id.judge_tv);
		mTitleTv = (TextView) view.findViewById(R.id.title_tv);

	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (commonClass == null || commonClass.getData() == null || commonClass.getCode() == null) {
			return;
		}
		List<InfoCardCompulsoryBean.DataBean> compulsoryBean = JSON.parseArray(commonClass.getData().toString() , InfoCardCompulsoryBean.DataBean.class);
		if (compulsoryBean == null || compulsoryBean.size() == 0) {
			mLoadingView.showEmptyView(true);
			mLoadingView.setEmptyListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					requestDatas();
				}
			});
			return;
		}
		if (compulsoryBean.get(0).getDefaultType() == 0) { //到货超时
			mTitleTv.setText(R.string.string_contract_info_card_timeout_arrival);
		} else if (compulsoryBean.get(0).getDefaultType() == 1) { //到货缺量
			mTitleTv.setText(R.string.string_contract_info_card_ullage_arrival);
		}
		if (StringUtils.isNoneNullString(compulsoryBean.get(0).getDefaultValueMin()) && StringUtils.isNoneNullString(compulsoryBean.get(0).getDefaultValueMax())) {
			mTimeTv.setText(compulsoryBean.get(0).getDefaultValueMin() + "小时-" + compulsoryBean.get(0).getDefaultValueMax() + "小时");
		}
		if (compulsoryBean.get(0).getRewandType() == 0) {
			mJudgeTv.setText(R.string.string_contract_info_card_tolerate);
		} else if (compulsoryBean.get(0).getRewandType() == 1) {
			if (StringUtils.isNullString(compulsoryBean.get(0).getRewandValue())) {
				return;
			}
			try {
				double rewandValue = Double.parseDouble(compulsoryBean.get(0).getRewandValue());
				mJudgeTv.setText("订单总费用打" + rewandValue / 10d + "折");
			} catch (NumberFormatException e) {
				e.printStackTrace();
				mJudgeTv.setText("订单总费用打" + "--"+ "折");
			}
		}

	}

	@Override
	public void onReLoad(int code) {

	}
}
